import { getFormFields } from '@/utils/formUtils'
import type { FormFieldConfig } from '@/types/form'
import { useDoctorStore } from '@/store/doctorStore'


// 表格列配置
export const tableColumns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '類型名稱',
    dataIndex: 'name',
  },
  {
    title: 'Code',
    dataIndex: 'code',
  },
  {
    title: '修改人',
    dataIndex: 'updatedBy',
  },
  {
    title: '修改時間',
    dataIndex: 'updatedAt',
  },
  {
    title: '操作',
    key: 'action',
    align: 'center',
  },
]


// 表單字段配置
const FormField: Record<string, FormFieldConfig> = {
  name: {
    label: '附加項目名稱',
    type: 'input',
    placeholder: '請輸入附加項目名稱',
    rules: [{ required: true, message: '請輸入附加項目名稱' }],
  },
  code: {
    label: 'Code',
    type: 'input',
    placeholder: '請輸入Code',
    rules: [{ required: true, message: '請輸入Code' }],
  }
}

export const getCreteFieldConfig = (fields?: string[]) => {
  return getFormFields(FormField, fields)
}

export const getEditFieldConfig =  (fields?: string[]) => {
  return getFormFields(FormField, fields)
}


//=============== SUB =======================


export const subTableColumns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '員工類型',
    dataIndex: 'staffType',
  },
  {
    title: '醫師',
    dataIndex: 'doctor',
  },
  {
    title: '分成方式',
    dataIndex: 'shareType',
  },
    {
    title: '分成',
    dataIndex: 'shareValue',
  },
  {
    title: '開始日期',
    dataIndex: 'validFrom',
  },
  {
    title: '結束日期',
    dataIndex: 'validTo',
  },
  {
    title: '修改人',
    dataIndex: 'updatedBy',
  },
  {
    title: '修改時間',
    dataIndex: 'updatedAt',
  },
  {
    title: '操作',
    key: 'action',
    align: 'center',
  },
]



// 表單字段配置
const SubFormField: Record<string, FormFieldConfig> = {
  staffType: {
    label: '醫師類型',
    type: 'select',
    placeholder: '請選擇醫師類型',
    options: [
      {
        label: '全部',
        key: 0,
      },
      {
        label: '合作',
        key: 1,
      },
      {
        label: '員工',
        key: 2,
      },
    ],
    defaultValue: 0,

  },
  staffCode: {
    label: '醫師',
    type: 'remoteSelect',
    placeholder: '請選擇醫師',
    searchFn: async (value: string) => {
      const doctorStore=useDoctorStore()
      const processData=await doctorStore.fetchOptions(value);
      if(!value?.trim()){
        return [{label: '全部（默認）',value: '', key: '',},...processData]
      }else{
        return processData
      }
    },
    options: [],
  },
  shareType: {
    label: '分成方式',
    type: 'radio',
    placeholder: '請選擇分成方式',
    options: [
      { label: '固定金額', key: 1 },
      { label: '按比例', key: 2 },
    ],
    defaultValue: 1,
  },
  shareValue: {
    label: '分成',
    type: 'number',
    placeholder: '請輸入分成',
    rules: [{ required: true, message: '請輸入分成', type: 'number' }],
    min: 0,
    addonBefore: (formState: any) => (formState.shareType === 1 ? '$' : ''),
    addonAfter: (formState: any) => (formState.shareType === 2 ? '%' : ''),
  },
  validDate: {
    label: '有效日期',
    type: 'daterange',
    placeholder: '請選擇有效日期',
  },
}


export const getSubCreteFieldConfig = async (fields?: string[]) => {
  SubFormField.staffCode.options = await SubFormField.staffCode.searchFn('')
  return getFormFields(SubFormField, fields)
}

export const getSubEditFieldConfig = async (fields?: string[]) => {
  SubFormField.staffCode.options = await SubFormField.staffCode.searchFn('')
  return getFormFields(SubFormField,fields)
}
